package com.shu.logsearch.jsch.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.shu.logsearch.common.base.BaseModel;
import com.shu.logsearch.util.AssertUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Objects;

/**
 * 远程服务器配置
 *
 * @author Tree Cai
 * @generator
 * @date 2019/07/10
 */
@Data
@TableName("remote_server_config")
@EqualsAndHashCode(callSuper = false)
public class RemoteServerConfig extends BaseModel {
    /**
     * 状态。删除-1，禁用0，启用1
     */
    @TableField("status")
    private Integer status;
    /**
     * 服务器名称
     */
    @TableField("server_name")
    private String serverName;

    /**
     * 描述
     */
    @TableField("description")
    private String description;

    /**
     * ip地址
     */
    @TableField("host")
    private String host;

    /**
     * 端口
     */
    @TableField("port")
    private Integer port;

    /**
     * 用户名
     */
    @TableField("user_name")
    private String userName;

    /**
     * 密码
     */
    @TableField("password")
    private String password;

    /**
     * 是否为跳板机。否0，是1
     */
    @TableField("is_jump_server")
    private Integer isJumpServer;
    public static Integer NOT_JUMPSERVER = 0;
    public static Integer JUMPSERVER = 1;

    /**
     * 目标服务器ip地址
     */
    @TableField("destination_host")
    private String destinationHost;

    /**
     * 目标服务器端口
     */
    @TableField("destination_port")
    private Integer destinationPort;

    /**
     * 目标服务器用户名
     */
    @TableField("destination_user_name")
    private String destinationUserName;

    /**
     * 目标服务器密码
     */
    @TableField("destination_password")
    private String destinationPassword;

    /**
     * 跳板机转发端口
     */
    @TableField("forward_local_port")
    private Integer forwardLocalPort;

    /**
     * 盐值，用于加密算法
     */
    @TableField("salt")
    private String salt;

    /**
     * 归属用户
     */
    @TableField("user_id")
    private String userId;

    /**
     * 日志文件目录
     */
    @TableField("file_path")
    private String filePath;

    /**
     * 判断是否为跳板机
     * @return
     */
    public boolean isJumpServer() {
        return Objects.equals(isJumpServer, JUMPSERVER);
    }

    /**
     * 检查连接远程服务器的参数是否配置完整
     *
     * @return
     */
    public boolean checkProperties() {
        try {
            AssertUtil.notNull(getHost(), getPort(), getUserName(), getPassword(), getUserId());
            if (isJumpServer())
                AssertUtil.notNull(getDestinationHost(), getDestinationPort(), getDestinationUserName(), getDestinationPassword());
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}